import { success,error,SQL_ERROR } from '@utils/response'
import redisDB from '@utils/redisconf'
import { sqlMain } from '@utils/dbconf'
const { Random } = require('mockjs')
import { province_list } from './static'
import adlogsql from '@maps/adminLog'
import usersql from '@maps/srcUser'
import infosql from '@maps/userInfo'
import cpttsql from '@maps/srcCptts'
import hiscpttsql from '@maps/srcHisevents'
const CPTT_IMG_PATH = 'file/cpttimg/'


export const dologin = async (req,res) =>{
try{
  const { data:{code},ipinfo:{ip,id:areaid} } = res.locals
  const { code:realcode } = await redisDB.get(0,'code')
  const tag = code === realcode
  // select * from test_limit order by id DESC limit 1;
  await sqlMain(adlogsql.add,[ip,areaid,Number(tag)])
  if(!tag)
    return res.send(error(401,'身份验证失败'))
  await redisDB.set(0,"ipinfo",{ip,areaid})
  const token = Random.string('lower',16)
  const logs = await sqlMain(adlogsql.select,[20])
  const dict = {}
  for(let log of logs){
    let select = dict[log.code]
    if(!select){
      select = await redisDB.get(0,log.code) || {name:province_list.find(e=>e.value == log.code.slice(0,2)).name,times:0}
      dict[log.code] = select
      const { name,times } = select
  //     console.log({log})
  //     await redisDB.set(0,log.code,{name,times:times+1})
  //     console.log("SET_OK")
    }
  //   console.log("OUT_1")
    log.name = select.name
    delete log.code
  //   console.log("OUT_2")
  }
  await redisDB.set(0,'token',{token})
  // areaid.slcie(0,2)
  return res.send(success({token,logs}))
}catch(e){
  console.log(e)
  return res.send(error(500,'我淦!'))}
}


export const selectuser = async (req,res) =>{
try{
  const { params:{pages,length,acc,email,authentic,type,isUse} } = res.locals
  const from = (Number(pages) - 1) * Number(length)//3,5，15,5
  let filter = ''
  if(acc) filter += `${filter? 'AND':''} ACCOUNT LIKE '%${acc}%' `
  if(email && email.length==1 ) filter += `${filter? 'AND':''} EMAIL IS ${Number(email[0])? 'NOT':''} NULL `
  if(authentic && authentic.length==1 ) filter += `${filter? 'AND':''} AUTHENTIC ${Number(authentic[0])? '!':''}= false `
  if(type && type.length==1 ) filter += `${filter? 'AND':''} TYPE=${type[0]} `
  if(isUse && isUse.length==1 ) filter += `${filter? 'AND':''} ISUSE=${isUse[0]} `
  const users = await sqlMain(usersql.select(filter),[from,Number(length)])
  const {total} = await sqlMain(usersql.gettotal(filter),[],true)
  for(let u of users)
    u.authentic = JSON.parse(u.authentic)
  // const {usernum:total} = await redisDB.get(4,"usernum")
  return res.send(success({users,total}))
}catch(e){
  return res.send(SQL_ERROR)}
}

export const getuserinfo = async (req,res) => {
try{
  const { params:{uid} } = res.locals
  const uinfo = await sqlMain(infosql.selectby,[uid],true)
  return res.send(success({uinfo}))
}catch(e){
  console.log(e)
  return res.send(SQL_ERROR)}
}

export const selectcptt = async (req,res) => {
try{
  const { params:{pages,length,type} } = res.locals
  const from = (Number(pages) - 1) * Number(length)
  if(type !== "true" && type !== "false") return res.send(error(500,'参数异常'))
  const cptts = await sqlMain(type?cpttsql.select:hiscpttsql.select,[from,Number(length)])
  const { total } = await sqlMain(type?cpttsql.gettotal:hiscpttsql.gettotal,[],true)
  console.log({total})
  for(let i of cptts){
    const { url,imgname } = i
    if(!url) i.url = `http://localhost:3000/${CPTT_IMG_PATH}${imgname}`
    delete i.imgname
  }
  return res.send(success({cptts,total}))
}catch(e){
  console.log(e)
  return res.send(SQL_ERROR)}
}

export const getcpttinfo = async (req,res) => {
try{
  return res.send(error(500,'没写'))
}catch(e){
  console.log(e)
  return res.send(SQL_ERROR)}
}